﻿// Copyright (c) Inossem. All rights reserved.
// Licensed under the LGPL-3.0 license. See LICENSE file in the project root for full license information.
using DWF.Studio.ScriptEngine.Custom;
using DWF.Studio.ScriptEngine.Implementation;
using DWF.Studio.ScriptEngine.IPC.Msg;
using DWF.Studio.ScriptEngine.Proxy;
using DWF.Studio.Share.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DWF.Studio.ScriptEngine.Loader
{
    public class WorkflowEngineProcessLoader : EngineProcessLoaderBase
    {
        public WorkflowEngineProcessLoader(ScriptEngineProxyContext engineContext)
            : base(engineContext)
        {
        }

        public override IScriptEngine Load(string[] resolutionPaths, ETargetPlatform platform)
        {
            return Load(resolutionPaths, null, platform);
        }

        public override IScriptEngine Load(string[] resolutionPaths, string[] assemblyNames, ETargetPlatform platform)
        {
            return Load(resolutionPaths, assemblyNames, String.Empty, platform);
        }

        public override IScriptEngine Load(string[] resolutionPaths, string[] assemblyNames, string pipeName, ETargetPlatform platform)
        {
            return Load(resolutionPaths, assemblyNames, null, pipeName, platform);
        }

        public override IScriptEngine Load(string engineId)
        {
            var proxy = new WorkflowScriptEngineProxy(engineId, EngineContext);

            return proxy;
        }

        public override IScriptEngine Load(string[] resolutionPaths, string[] assemblyNames, Dictionary<string, string> projectProperties, string pipeName, ETargetPlatform platform)
        {
            Tuple<string[], string[], Dictionary<string, string>> args = new Tuple<string[], string[], Dictionary<string, string>>(resolutionPaths, assemblyNames, projectProperties);
            SetEngineArguments(JsonHelper.Serialize(args));
            StartProcess($"WorkflowScriptEngine {pipeName} {Process.GetCurrentProcess()?.Id}", platform, projectProperties);
            var Id = EngineContext.WaitEcho();

            var proxy = new WorkflowScriptEngineProxy(Id, EngineContext);

            return proxy;
        }
    }
}
